/**
 * @(#)JFrameTest.java 2011-11-7 Copyright 2011 it.kedacom.com, Inc. All rights
 *                     reserved.
 */

package com.kedacom.xmpp.demo.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class JFrameTest extends JFrame {

	private final JCheckBox ck1, ck2, ck3, ck4;
	private final JButton btnShowDialog;
	private MessageDialog md;

	public JFrameTest(String title) {
		this.setTitle(title);
		this.setLayout(new FlowLayout());
		ck1 = new JCheckBox("睡觉");
		ck2 = new JCheckBox("游泳");
		ck3 = new JCheckBox("Cooking");
		ck4 = new JCheckBox("爬山");

		btnShowDialog = new JButton("弹出对话框");

		this.add(ck1);
		this.add(ck2);
		this.add(ck3);
		this.add(ck4);
		this.add(btnShowDialog);

		btnShowDialog.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				md = new MessageDialog(JFrameTest.this, "信息", true);
				md.setVisible(true);
			}
		});

		this.setVisible(true);
		this.setSize(500, 300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void showChange(int flagCheck) {
		if (flagCheck == 2) {
			// 全选
			ck1.setSelected(true);
			ck2.setSelected(true);
			ck3.setSelected(true);
			ck4.setSelected(true);
		} else {
			ck1.setSelected(false);
			ck2.setSelected(false);
			ck3.setSelected(false);
			ck4.setSelected(false);
		}
	}

	public static void main(String[] args) {
		new JFrameTest("bendan");
	}

}


class MessageDialog extends JDialog {

	private final JButton btnOk;
	private final JCheckBox ck;
	private int flagCheck;
	private final JFrameTest frame;

	public MessageDialog(JFrameTest owner, String title, boolean modal) {
		super(owner, title, modal);
		this.frame = owner;
		this.setLayout(new BorderLayout());
		ck = new JCheckBox();
		ck.setText("选择所有");
		btnOk = new JButton("确认选择");
		btnOk.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				if (ck.isSelected()) {
					flagCheck = 2;
				} else {
					flagCheck = 1;
				}
				frame.showChange(flagCheck);
				MessageDialog.this.dispose();
			}
		});
		this.add(ck, BorderLayout.CENTER);
		this.add(btnOk, BorderLayout.SOUTH);
		this.setSize(200, 150);
	}
}
